package com.goldsign.pay.provider.user.service.impl;

import com.goldsign.pay.common.exception.GcServiceException;
import com.goldsign.pay.common.log.SysLog;
import com.goldsign.pay.common.service.impl.BaseServiceImpl;
import com.goldsign.pay.common.string.StringUtil;
import com.goldsign.pay.common.util.Constant;
import com.goldsign.pay.provider.user.service.UserLoginService;
import com.goldsign.pay.provider.user.service.UserService;
import com.goldsign.pay.provider.user.to.UserLoginReqDTO;
import com.goldsign.pay.provider.user.to.UserLoginRespDTO;
import com.goldsign.pay.provider.user.util.AppLoginToken;
import com.goldsign.pay.provider.user.util.AppTokenManager;
import com.goldsign.pay.provider.user.util.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by xiaojingjing on 2018/5/17.
 */
@Service
public class UserLoginServiceImpl extends BaseServiceImpl<UserLoginReqDTO> implements UserLoginService{

    @Autowired
    UserService userService;

    @Override
    public UserLoginRespDTO userLogin(UserLoginReqDTO req) {

        SysLog.info("[收到用户登录请求]" + req);

        UserLoginRespDTO resp = new UserLoginRespDTO();

        String id = null;
        try {
            if(StringUtils.isEmpty(req.getLogin_id())||StringUtils.isEmpty(req.getLogin_passwd())){
                resp.setResp_code(Constant.PARAMETER_ERROR);
                resp.setResp_msg("PARAMETER_ERROR");
                SysLog.info("参数不能为空：login_id=" + req.getLogin_id() + "login_passwd=" + req.getLogin_passwd());
                return resp;
            }

            //记录登录日志
            id = userService.insertLoginLog(req);

            //调用用户基础服务，校验登录信息
            String userNo = userService.checkLoginPasswd(req, id);

            if (StringUtil.isEmpty(userNo)) {
                resp.setResp_code(Constant.UNCORRECT_USERNAME_OR_PASSWORD);
                resp.setResp_msg("UNCORRECT_USERNAME_OR_PASSWORD");
                return resp;
            }

            resp.setUserNo(userNo);
            resp.setResp_code(Constant.SUCCESS);
            resp.setResp_msg("SUCCESS");
        } catch (GcServiceException e) {
            resp.setResp_code(e.getErrorCode());
            resp.setResp_msg(e.getMessage());
        }catch (Exception e) {
            SysLog.error(e.getMessage());

            resp.setResp_code(Constant.SYSTERM_ERROR);
            resp.setResp_msg("SYSTERM_ERROR");
        }finally {
            if(id != null && resp!= null ) {
                //更新登录结果
                try {

                    userService.updateLoginLog(id, resp.getResp_code());
                } catch (GcServiceException e1) {
                    SysLog.error(e1.getMessage());
                }
            }
        }

        SysLog.info("[返回用户登录响应]" + resp);
        return resp;
    }


    @Override
    public User handleResult(UserLoginRespDTO userLoginResp, UserLoginReqDTO userLoginReq){
        User user = new User();
        //登录成功
        if (userLoginResp.getResp_code().equals(Constant.SUCCESS)) {
            //生成token
            AppLoginToken newToken = new AppLoginToken(userLoginReq.getLogin_id(), userLoginReq.getLogin_passwd());
            AppTokenManager.addNewToken(userLoginReq.getLogin_id(), newToken);
            user = new User(newToken);
            newToken.setAttrubute("LoginUser", userLoginResp);
        }
        return user;
    }

    @Override
    public void appLogout(String tokenId) throws Exception {
        UserLoginRespDTO req = AppTokenManager.getLoginUser(tokenId);
        if(req != null) {
            AppTokenManager.removeToken(req.getUserNo());
        }
    }
}
